import { createReducer, createAsyncAction } from 'redux-action-tools';
import axios from 'axios'
const appInstance = axios.create({
  baseURL: 'http://localhost:5000/',
  timeout: 1000,
  headers: {
    'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjU3ZmNhODc3NTIxNDA1MjM5NDJjNmU0NSIsImlhdCI6MTQ4MTg1NDE4Mn0.oP_2Vd2C22aEN6Xp0qI5Hs92HXsNasPCuKPlr4ZAKhQ',
     "accept": "application/json"
  }
});
// ------------------------------------
// 这里为所有跟答题期数有关热reducer
// ------------------------------------


const GETALLTERMS = 'GETALLTERMS',
      GETONETERMBYID = 'GETONETERMBYID',
      ADDONETERM='ADDONETERM',
      UPDATEONETERMBYID = 'UPDATEONETERMBYID',
      DELETEONETERMBYID = 'DELETEONETERMBYID';

export const getAllTerms = createAsyncAction(GETALLTERMS, () => {

  return appInstance.get('/terms').then(response=>Promise.resolve(response.data))

})
const GETALLTERMS_SUCCESS = (state, action) => {
  console.log(action);
  return Object.assign({}, state, { termslist: action.payload.data})
}


export const getOneTermByID = createAsyncAction(GETONETERMBYID, (id) => {
  return appInstance.get(`/terms/${id}`).then(response=>Promise.resolve(response.data))
})

const GETONETERMBYID_SUCCESS = (state, action) => {
  const answerlist = action.payload.data;
  return Object.assign({}, state,  { answerlist: answerlist })
}

export const updateOneTermById = createAsyncAction(UPDATEONETERMBYID, (term) => {
  return appInstance.put(`/terms/${term._id}`,{term:term}).then(response=>Promise.resolve(response.data))

})
const UPDATEONETERMBYID_SUCCESS = (state, action) => {
  let newterm=action.payload.data;
  console.log(newterm)
   return Object.assign({}, state,  { termslist: state.termslist.map(term=>{
      if(newterm._id===term._id){
        console.log(newterm,term)
        return newterm
      }else{
      return term
        
      }
   }) });
}


export const deleteOneTermById = createAsyncAction(DELETEONETERMBYID, (termId) => {
  console.log(termId);
  return appInstance.delete(`/terms/${termId}`).then(response=>Promise.resolve(response.data))

})
const DELETEONETERMBYID_SUCCESS = (state, action) => {
  console.log("请求成功了",action);

  let termslist=state.termslist.filter(term=>{
    return term._id!==action.payload.data._id;
  });
   return Object.assign({}, state, { termslist:termslist,termsLoading:false })
}


export const addOneTerm = createAsyncAction(ADDONETERM, (term) => {
  console.log(term)
   return appInstance.post(`/terms/`,{term:term}).then(response=>Promise.resolve(response.data))

})
const ADDONETERM_SUCCESS = (state, action) => {
  // console.log("请求成功了",action,action.payload.data.terms);

  return Object.assign({}, state, { termslist:[  action.payload.data,...state.termslist],termsLoading:false })
}



const getError = (state, action) => {
  console.log("请求失败了");
  return state
}

const answerListReducer = createReducer()
  .when(GETALLTERMS)
  .done(GETALLTERMS_SUCCESS)
  .when(UPDATEONETERMBYID)
  .done(UPDATEONETERMBYID_SUCCESS)
  .when(GETONETERMBYID)
  .done(GETONETERMBYID_SUCCESS)
  .when(ADDONETERM)
  .done(ADDONETERM_SUCCESS)
  .when(DELETEONETERMBYID)
  .done(DELETEONETERMBYID_SUCCESS)
  .failed(getError)
  .build({ termslist: [], termData: [],termsLoading:false });

export default answerListReducer



// export  const oneAnswerReducer= createReducer()
//  .when(GETALLTERMS)
//  .done(GETALLTERMS_SUCCESS)
//  .failed(getError)
//  .build({ answerslist: [] });
